﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EasyStock
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (CommonClass.dicStock == null)
            {
                CommonClass.dicStock = new Dictionary<string, Stock>();
            }
        }

        private void btnAddCode_Click(object sender, EventArgs e)
        {
            if (!validateInput())
                return;
            ListViewItem lvi = new ListViewItem(cboCode1.Text);
            lvCode.Items.Add(lvi);
            lvi.SubItems.AddRange(new string[] { txtStartCode.Text, txtEndCode.Text });
        }

        private bool validateInput()
        {
            if (cboCode1.Text == "")
            {
                MessageBox.Show("请选择代码类型");
                return false;
            }
            if (txtStartCode.Text.Trim().Length != 6)
            {
                MessageBox.Show("请输入正确的起始代码");
                return false;
            }
            if (txtEndCode.Text.Trim().Length != 6)
            {
                MessageBox.Show("请输入正确的结束代码");
                return false;
            }
            int start = 0;
            int end = 0;
            try
            {
                start = int.Parse(txtStartCode.Text.Trim());
            }
            catch
            {
                MessageBox.Show("请输入正确的起始代码");
                return false;
            }
            try
            {
                end = int.Parse(txtEndCode.Text.Trim());
            }
            catch
            {
                MessageBox.Show("请输入正确的结束代码");
                return false;
            }
            if (start > end)
            {
                MessageBox.Show("请输入正确的结束代码");
                return false;
            }
            return true;
        }

        private void tsmiDelete_Click(object sender, EventArgs e)
        {
            lvCode.Items.Remove(lvCode.SelectedItems[0]);
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            CommonClass.dicStock.Clear();
            string code = "";
            foreach (ListViewItem lvi in lvCode.Items)
            {
                try
                {
                    for (int i = int.Parse(lvi.SubItems[1].Text.Trim()); i <= int.Parse(lvi.SubItems[2].Text.Trim()); i++)
                    {
                        string stri = i.ToString();
                        while (stri.Length < 6)
                        {
                            stri = "0" + stri;
                        }
                        if (code != "")
                            code += ",";
                        code += lvi.SubItems[0].Text.Trim() + stri;
                        if (code.Length > 1400)
                        {
                            CommonClass.getStockByCode(code);
                            code = "";
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("数据出错，请联系管理员！");
                    return;
                }
            }
            if (code != "")
            {
                CommonClass.getStockByCode(code);
                code = "";
            }
            refreshStock();
        }

        private void refreshStock()
        {
            lvStock.Items.Clear();
            int i = 1;
            foreach (Stock stock in CommonClass.dicStock.Values)
            {
                ListViewItem lvi = new ListViewItem(i.ToString());
                lvStock.Items.Add(lvi);
                lvi.SubItems.AddRange(new string[] { stock.Code, stock.StockName, stock.Volume, stock.Close, stock.Openning, stock.NowPrice, stock.MaxPrice, stock.MinPrice });
                i++;
            }
        }
        private void refreshStock(List<KeyValuePair<string, Stock>> list)
        {
            lvStock.Items.Clear();
            int i = 1;
            foreach (KeyValuePair<string, Stock> item in list)
            {
                ListViewItem lvi = new ListViewItem(i.ToString());
                lvStock.Items.Add(lvi);
                lvi.SubItems.AddRange(new string[] { item.Value.Code, item.Value.StockName, item.Value.Volume, item.Value.Close, item.Value.Openning, item.Value.NowPrice, item.Value.MaxPrice, item.Value.MinPrice });
                i++;
            }
        }

        private void tsmiCode0_Click(object sender, EventArgs e)
        {
            List<KeyValuePair<string, Stock>> list = CommonClass.dicSort("Code", 0);
            refreshStock(list);
        }

        private void tsmiCode1_Click(object sender, EventArgs e)
        {
            List<KeyValuePair<string, Stock>> list = CommonClass.dicSort("Code", 1);
            refreshStock(list);
        }

        private void tsmiVolume0_Click(object sender, EventArgs e)
        {
            List<KeyValuePair<string, Stock>> list = CommonClass.dicSort("Volume", 0);
            refreshStock(list);
        }

        private void tsmiVolume1_Click(object sender, EventArgs e)
        {
            List<KeyValuePair<string, Stock>> list = CommonClass.dicSort("Volume", 1);
            refreshStock(list);
        }
    }
}
